package com.ltmall.api.comm;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ltmall.api.comm.json.City;

import java.io.*;
import java.util.*;
import java.util.Map.Entry;

public class CityParse {
    private static List<City> cityList = new ArrayList<>();

    /**
     * 注意filepath的内容
     */
    static String filePath = "resource" + File.separator + "city.json";

    /**
     * 注意filepath的内容
     */
    static String constantPath = "resource" + File.separator + "city.json";

    public static String readFile() {
        BufferedReader reader = null;
        String laststr = "";
        try {
            FileInputStream fileInputStream = new FileInputStream(filePath);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
            reader = new BufferedReader(inputStreamReader);
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                laststr += tempString;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return laststr;
    }

    public static void initJsonPath(String path) {
        if (filePath.equals(constantPath)) {
            filePath = path + filePath;
        }
    }

    @SuppressWarnings("unchecked")
    public static void excute() {
        String JsonContext = readFile();

        JSONArray jsonArray = JSONArray.parseArray(JsonContext);

        int size = jsonArray.size();
        System.out.println("Size: " + size);

        for (int i = 0; i < size; i++) {

            City c = new City();
            JSONObject jObject = jsonArray.getJSONObject(i);
            String section = jObject.getString("n");
            JSONArray array = jObject.getJSONArray("s");

            c.setA(section);

            List<Map<String, List<String>>> B = new ArrayList<Map<String, List<String>>>();

            for (int j = 0; j < array.size(); j++) {

                Map<String, List<String>> m = new HashMap<String, List<String>>();
                JSONObject obj1 = array.getJSONObject(j);
                String section1 = obj1.getString("n");
                Object bj = obj1.get("s");
                List<String> list = (List<String>) bj;
                m.put(section1, list);
                B.add(m);
            }
            c.setB(B);
            cityList.add(c);
        }
    }

    public static boolean isZone(String addr) {
        if (cityList.size() == 0) {
            excute();
        }
        for (City city : cityList) {
            if (city.getA().equals(addr)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isProvince(String addr) {
        if (cityList.size() == 0) {
            excute();
        }

        for (City city : cityList) {
            String zone = city.getA();
            List<Map<String, List<String>>> citys = city.getB();
            for (Map<String, List<String>> map : citys) {
                Iterator<Entry<String, List<String>>> it = map.entrySet().iterator();
                while (it.hasNext()) {
                    Entry<String, List<String>> entry = it.next();
                    if (entry.getKey().equals(addr)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public static Map<String, String> isCity(String addr) {
        if (cityList.size() == 0) {
            excute();
        }

        Map<String, String> returnMap = new HashMap<String, String>();

        for (City city : cityList) {
            String zone = city.getA();
            List<Map<String, List<String>>> citys = city.getB();
            for (Map<String, List<String>> map : citys) {
                Iterator<Entry<String, List<String>>> it = map.entrySet().iterator();
                while (it.hasNext()) {
                    Entry<String, List<String>> entry = it.next();
                    String province = entry.getKey();
                    List<String> cityList = entry.getValue();
                    for (String c : cityList) {
                        if (c.equals(addr)) {
                            returnMap.put(zone, province);
                        }
                    }
                }
            }
        }
        return returnMap;
    }


}